package com.shop2cn.iapi.agentproducts.testcase;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ImportAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.OpenCatalogDto;
import com.shop2cn.iapi.agentproducts.parameter.OpenProductDto;
import com.shop2cn.iapi.agentproducts.parameter.QueryProductListBean;
import com.shop2cn.iapi.agentproducts.service.QueryProductListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AgentProductCallServiceV2;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

/**
*获取商户商品列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_QueryProductList{
	private static QueryProductListBean queryproductlistBean;
	private static QueryProductListCall queryproductlistCall;
	//渠道商 
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";

	//vip渠道
	private static int vipRootUserId = AgentUsers.vipRootUserId;
	private static String vipRootUserName = "NickautoTest";
	//vip渠道->代理
	private static int vipAgentUserId1 = AgentUsers.vipAgentUserId1;
	private static String vipAgentUserName1 = "dgsq408897016";
	//vip渠道->代理->代理
	private static int vipAgentUserId2 = AgentUsers.vipAgentUserId2;
	private static String vipAgentUserName2 = "NickautoTest";
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取商户商品列表");
	}

	@Before
	public void caseUp() {
		queryproductlistBean=new QueryProductListBean();
		queryproductlistCall=new QueryProductListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(queryproductlistCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_01() {
		Logger.start(true,"渠道商获取商户商品列表，不传productIdList，根据开始和结束时间来获取商户商品列表");
		try {
			queryproductlistBean.setUserId(rootUserId);
			List<String> productIdList = new ArrayList<>();
			queryproductlistBean.setProductIdList(productIdList);
			queryproductlistBean.setPageNo(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistBean.setStartUpdateTime(YMTDateUtil.getBeforeOrNextDay(-1));
			queryproductlistBean.setEndUpdateTime(YMTDateUtil.getBeforeOrNextDay(1));
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");
			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}


	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_02() {
		Logger.start(true,"渠道商获取商户商品列表，根据productIdList来获取商户商品列表");
		try {
			queryproductlistBean.setUserId(rootUserId);
			List<String> productIdList = new ArrayList<>();
			for(int i=0;i<5;i++){
				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				productIdList.add(productId);
			}
			queryproductlistBean.setProductIdList(productIdList);

			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");

			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_05() {
		Logger.start(true,"渠道商获取商户商品列表，根据productIdList来获取商户商品列表，productList长度超过50个，获取失败");
		try {
			queryproductlistBean.setUserId(rootUserId);
			List<String> productIdList = new ArrayList<>();
			for(int i=0;i<51;i++){
				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				productIdList.add(productId);
			}
			queryproductlistBean.setProductIdList(productIdList);

			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("400",queryproductlistCall.getString("code"),"验证code");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_03() {
		Logger.start(true,"获取商户商品列表，根据productIdList来获取商户已经删除的商品列表，获取失败");
		try {
			queryproductlistBean.setUserId(rootUserId);
			List<String> productIdList = new ArrayList<>();
			for(int i=0;i<5;i++){
				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,rootUserName,false);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				AddAgentProductCallService.DeleteProduct(productId,rootUserId);
				productIdList.add(productId);
			}
			queryproductlistBean.setProductIdList(productIdList);

			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");

			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			Logger.verifyEquals(0,openProductDtos.size(),"已删除商品没有返回值");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}


	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_06() {
		Logger.start(true,"代理商获取商品列表，不传productIdList");
		try {
			queryproductlistBean.setUserId(vipAgentUserId1);
			List<String> productIdList = new ArrayList<>();
			queryproductlistBean.setProductIdList(productIdList);
			queryproductlistBean.setPageNo(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistBean.setStartUpdateTime(YMTDateUtil.getBeforeOrNextDay(-1));
			queryproductlistBean.setEndUpdateTime(YMTDateUtil.getBeforeOrNextDay(1));
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");
//			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
//			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_07() {
		Logger.start(true,"代理商商获取商户商品列表，根据productIdList来获取商户商品列表");
		try {
			queryproductlistBean.setUserId(rootUserId);
			List<String> productIdList = new ArrayList<>();
			for(int i=0;i<5;i++){
				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,false);
				String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
				productIdList.add(agentProductId);
			}
			queryproductlistBean.setProductIdList(productIdList);

			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");

			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_08() {
		Logger.start(true,"代理商商获取商户商品列表，根据productIdList来获取商户商品列表，渠道商删除商品，代理商不可见");
		try {
			queryproductlistBean.setUserId(vipAgentUserId1);
			List<String> productIdList = new ArrayList<>();
			List<String> agentProductIdList = new ArrayList<>();
			for(int i=0;i<5;i++){
				AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,true);
				String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
				productIdList.add(productId);
				ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,false);
				String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
				agentProductIdList.add(agentProductId);
			}
			queryproductlistBean.setProductIdList(agentProductIdList);

			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");

			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);

			productIdList.forEach(p->{
				try {
					AddAgentProductCallService.DeleteProduct(p,vipRootUserId);
				} catch (IOException e) {
					e.printStackTrace();
				}
			});
			queryproductlistBean.setProductIdList(agentProductIdList);

			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");

			List<OpenProductDto> openProductDtos1 = queryproductlistCall.getProductList();
			Logger.verifyEquals(0,openProductDtos1.size(),"已删除商品没有返回值");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}


	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_09() {
		Logger.start(true,"渠道商没有设置商品下级代理价格，获取代理商商品详情");
		try {
			queryproductlistBean.setUserId(vipAgentUserId1);
			List<String> productIdList = new ArrayList<>();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);
			productIdList.add(agentProductId);
			queryproductlistBean.setProductIdList(productIdList);

			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");

			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_10() {
		Logger.start(true,"vip渠道->代理1->代理2,渠道商没有设置商品下级代理价格，获取vip2商品详情");
		try {
			queryproductlistBean.setUserId(vipAgentUserId2);
			List<String> productIdList = new ArrayList<>();
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(vipRootUserId,vipRootUserName,false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);

			ImportAgentProductBean importAgentProductBean = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId1,vipAgentUserName1,productId,vipRootUserId,false);
			String agentProductId = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean);

			ImportAgentProductBean importAgentProductBean1 = AddAgentProductCallService.initImportAgentProduct(vipAgentUserId2,vipAgentUserName2,agentProductId,vipAgentUserId1,false);
			String agentProductId1 = AddAgentProductCallService.ImportAgentProduct(importAgentProductBean1);
			productIdList.add(agentProductId1);
			queryproductlistBean.setProductIdList(productIdList);

			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");

			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_011() {
		Logger.start(true,"渠道商获取商户商品列表，根据SKU来获取商户商品列表");
		try {
			queryproductlistBean.setUserId(rootUserId);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			queryproductlistBean.setSku(addAgentProductBean.getCatalogList().get(0).getSku());

			queryproductlistBean.setPageNo(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");
			
			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			
			Logger.verifyEquals(productId, openProductDtos.get(0).getProductId(), "验证商品id");
			
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_012() {
		Logger.start(true,"渠道商获取商户商品列表，根据品牌id来获取商户商品列表");
		try {
			queryproductlistBean.setUserId(rootUserId);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			queryproductlistBean.setBrandId(addAgentProductBean.getBrandId());
			queryproductlistBean.setPageNo(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService();
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");
			
			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			
			Logger.verifyEquals(productId, openProductDtos.get(0).getProductId(), "验证商品id");
			
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_013() {
		Logger.start(true,"渠道商获取商户商品列表，根据类目id来获取商户商品列表");
		try {
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			List<String> barCodes = Arrays.asList("hellohi"+TestDataManager.randomStr(10),"hihello"+TestDataManager.randomStr(10));
			addAgentProductBean.getCatalogList().get(0).setBarcode(barCodes.get(0));;
			addAgentProductBean.getCatalogList().get(1).setBarcode(barCodes.get(1));;
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			queryproductlistBean.setCategoryId(addAgentProductBean.getCategoryId());
			queryproductlistBean.setUserId(rootUserId);
			queryproductlistBean.setPageNo(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService(); 
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");
			
			
			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			
			Logger.verifyEquals(productId, openProductDtos.get(0).getProductId(), "验证商品id");
			List<OpenCatalogDto> skuList = openProductDtos.get(0).getSkuList();
			Logger.verifyEquals(barCodes.get(0), skuList.get(0).getBarcode(), "验证条形码");
			Logger.verifyEquals(barCodes.get(1), skuList.get(1).getBarcode(), "验证条形码");
			
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_QueryProductList_014() {
		Logger.start(true,"渠道商获取商户商品列表，新增字段 barcode");
		try {
			queryproductlistBean.setUserId(rootUserId);
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, false);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			
			queryproductlistBean.setCategoryId(addAgentProductBean.getCategoryId());
			queryproductlistBean.setPageNo(1);
			queryproductlistBean.setPageSize(10);
			queryproductlistCall.setData(queryproductlistBean);
			queryproductlistCall.callService(); 
			Logger.verifyEquals("200",queryproductlistCall.getString("code"),"验证code");
			
			
			List<OpenProductDto> openProductDtos = queryproductlistCall.getProductList();
			
			Logger.verifyEquals(productId, openProductDtos.get(0).getProductId(), "验证商品id");
			
			AgentProductCallServiceV2.checkQueryProductList(openProductDtos,queryproductlistBean);
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	


}